/*
 * Package il.ac.biu.cs.grossmm.test.test2
 * File Subscriber.java
 * 
 * This is a part of presence service framework API. 
 * See javadoc for more information.
 *  
 * Copyright (C) 2006 Grossmann Mark
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package il.ac.biu.cs.grossmm.tests.test2;

import static il.ac.biu.cs.grossmm.api.data.NodeTypeByInterface.*;
import static il.ac.biu.cs.grossmm.api.keys.KeyFactory.*;
import static il.ac.biu.cs.grossmm.api.keys.PatternFactory.*;
import static il.ac.biu.cs.grossmm.api.presence.BaseVocabulary.*;
import static il.ac.biu.cs.grossmm.api.presence.ResourceType.*;
import il.ac.biu.cs.grossmm.api.data.Root;
import il.ac.biu.cs.grossmm.api.flow.ActiveDataManager;
import il.ac.biu.cs.grossmm.api.flow.NodeEventInspector;
import il.ac.biu.cs.grossmm.api.flow.SubscriptionPoint;
import il.ac.biu.cs.grossmm.api.flow.Status;
import il.ac.biu.cs.grossmm.api.keys.Key;
import il.ac.biu.cs.grossmm.api.server.Component;
import il.ac.biu.cs.grossmm.api.server.ComponentManager;
import il.ac.biu.cs.grossmm.api.server.ServiceManager;
import il.ac.biu.cs.grossmm.api.server.Service;

import java.util.logging.Logger;

public class Subscriber extends Thread implements
	il.ac.biu.cs.grossmm.api.flow.Subscriber<FooPresence>, Component, Service {
	Logger logger = Logger.getLogger("Subscriber");

	ActiveDataManager adm;

	SubscriptionPoint<FooPresence> sp;

	private ServiceManager serviceManager;

	public void activated(Key key, Root<FooPresence> node)
	{
		logger.info("Activated " + key);
	}

	public void deactivated(Key key, Status status)
	{
		logger.info("Deactivated " + key);
	}

	public void notify(Root<FooPresence> node, NodeEventInspector inspector)
	{
//		logger.info("Notification " + node.getRoot().getKey()
//			+ inspector.toString(node.getRoot()));
	}

	public void aborted(Object hint, Exception e) throws Exception
	{
		throw e;
	}

	public void setComponentManager(ComponentManager container)
		throws Exception
	{
		adm =
			(ActiveDataManager) container.getComponent(ActiveDataManager.class);
		sp =
			adm.createSubscriptionPoint(this, pattern(String.class, mandatory(
				RESOURCE_TYPE, PRESENCE)), nodeType(FooPresence.class));
	}

	public void start(ServiceManager serviceManager) throws Exception
	{
		this.serviceManager = serviceManager;
		serviceManager.requireService("Notifier");
		logger.info("Started");

		start();
	}

	public void stop(Exception exception)
	{
		serviceManager.releaseService("Notifier");
		logger.info("Stopped");
	}

	public void terminated(Object serviceHint, Exception e) throws Exception
	{
		throw e;
	}

	@Override
	public void run()
	{
		boolean b;

		for (int i = 0; i < 100000; i++) {
			if( i%1000 == 0) 
				System.out.println("Progress: "+i);
			
			for (int j = 0; j < 50; j++) {
				Key key = key("Alice" + i, entry(RESOURCE_TYPE, PRESENCE));
				b = sp.subscribe(key, null) == Status.UNMATCHED;
				// logger.info("b="+b);
				// logger.info("state="+sp.getSubscriptionState(key));

				// Root<FooPresence> fooRoot = sp.getNode(key);
				//				
				// if( fooRoot != null ) {
				// NodeInspector ins = sp.readLock(fooRoot);
				// logger.info(ins.toString(fooRoot));
				// sp.readUnlock(fooRoot);
				// }
			}
		}
	}
}
